/**
 * mysql封装
 */
const mysql = require('mysql');
const Connection = require('mysql/lib/Connection');
const {Promise} = require('bluebird');
const {coreMysqlConfig, nytMysqlConfig} = require('../config').config;
// 创建数据池(核心平台)
const corePool = mysql.createPool(coreMysqlConfig);
//创建农业厅的数据库连接池
const nytPool = mysql.createPool(nytMysqlConfig);

/**
 * 获取核心平台的数据库连接
 * @returns {Promise<void>}
 */
async function getCoreConnection() {
    return new Promise((resolve, reject) => {

        corePool.getConnection((err, connection) => {
            if (err) {
                reject(err);
            } else {
                resolve(connection);
            }
        });
    });
}

/**
 * 开始事务
 * @param connection
 * @returns {Promise<void>}
 */
async function beginTX(connection) {
    transAutoRelease(connection);
    return new Promise((resolve, reject) => {

        connection.beginTransaction(null, (err) => {
            if (err) {
                reject(err);
            } else {
                resolve(connection);
            }
        });
    });
}

/**
 * 执行sql
 * @param {数据库连接} connection
 * @param {要执行的sql} sql
 * @param { 要执行的sql 需要的参数} params
 */
async function query(connection, sql, params) {
    return new Promise((resolve, reject) => {
        if (params) {
            connection.query(sql, params, (err, rows, fields) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({rows: rows, fields: fields});
                }
            });
        } else {
            connection.query(sql, (err, rows, fields) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({rows: rows, fields: fields});
                }
            });
        }

    });
}

/**
 * 插入数据
 * @param {执行sql需要的mysql连接} connection
 * @param {需要执行的sql语句} sql
 * @param {需要插入的数据} params
 */
async function insert(connection, sql, params) {
    return new Promise((resolve, reject) => {
        connection.query(sql, params, (err, result) => {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });
}

/**
 * 获取农业厅的数据库连接
 * @returns {Promise<void>}
 */
async function getNytConnection() {
    return new Promise((resolve, reject) => {

        nytPool.getConnection((err, connection) => {
            if (err) {
                reject(err);
            } else {
                resolve(connection);
            }
        });
    });
}


function after(fn, cb) {
    return function () {
        fn.apply(this, arguments);
        cb();
    };
}

function transAutoRelease(conn) {
    if (conn.commit == Connection.prototype.commit)
        conn.commit = after(conn.commit, release);
    if (conn.rollback == Connection.prototype.rollback)
        conn.rollback = after(conn.rollback, release);

    function release() {
        if (conn) {
            conn.release();
        }
    }
}
/**
 * 销毁连接池
 */
function endPool(){
    corePool.end();
    nytPool.end();
}
module.exports.getCoreConnection = getCoreConnection;
module.exports.getNytConnection = getNytConnection;
module.exports.query = query;
module.exports.insert = insert;
module.exports.endPool = endPool;
module.exports.beginTX = beginTX;